<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
  <title></title>
  <!-- build:css /css/main.css -->
  <link href="/css/mui.css" rel="stylesheet"/>
  <link href="/css/iconfont.css" rel="stylesheet"/>
  <link href="/css/common.css" rel="stylesheet"/>
  <!-- endbuild -->
  <style>
    #orderProductList {
      margin-top: 8px;
    }

    #orderProductList.product-list .mui-table-view-cell {
      padding: 10px 18px;
      border-bottom: 1px solid #eee;
      background: #fff;
    }

    #orderProductList.product-list.mui-table-view .mui-media-body {
      width: 60%;
      margin-top: 12px;
    }

    #orderProductList.product-list.mui-table-view .mui-media-object {
      width: 60px;
      line-height: 60px;
      max-width: 60px;
      max-height: 60px;
      height: 60px;
    }
  </style>
</head>

<body>
<header class="mui-bar mui-bar-nav u-nav">
  <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
  <h1 class="mui-title">我的订单</h1>
</header>
<div class="mui-content mui-scroll-wrapper" id="pullrefresh">
  <div class="mui-scroll" id="hotProductList">

    <ul class="mui-table-view product-list order-product-list" id="orderProductList">
      <!--<li class="mui-table-view-cell mui-media">
                    <p class="">
                        <font>订单号：2015111264181678</font>
                        <font class="fr order-ok">交易成功</font>
                    </p>
                    <a href="order-info.html">
                        <img class="mui-media-object mui-pull-left" src="https://cdn.bestkeep.cn/goods/img/goods_1484287899733.jpg@60p_80q">
                        <div class="mui-media-body">幸福就是可以一起睡觉幸福就是可以一起睡觉幸福就是可以一起睡觉幸福就是可以一起睡觉</div>
                        <div class="price-position"><span class="money">￥5</span><span class="num">x1</span></div>
                    </a>
                    <div  class="ordertt">共<span class="font15">1</span>件商品&nbsp;&nbsp;合计：<span class="font15">￥7.98</span>(含运费<span class="font15"> ￥4.50</span>)</div>
                <div  class="ordertt"><a>删除订单</a><a>查看物流</a><a class="pay">去支付</a></</div>
                </li>-->
    </ul>

  </div>
</div>
<!-- build:js /js/vendor.js -->
<script src="/js/mui.min.js"></script>
<!-- bower:js -->
<script src="/bower_components/mockjs/dist/mock.js"></script>
<script src="/bower_components/zepto/zepto.js"></script>
<script src="/bower_components/Zepto-Cookie/zepto.cookie.js"></script>
<!--  endbower -->
<!-- endbuild -->
<!-- build:js /js/main.js -->
<script src="/js/common.js"></script>
<script src="/js/app.js"></script>
<!-- endbuild -->
<script>
  var tokenVal = $.fn.cookie('token');
  ifLogin(tokenVal);
  var type = getQueryString('type');
  var page = 1;
  (function (mui, doc) {
    var user = app.getUsers();
    mui.init({
      pullRefresh: {
        container: "#pullrefresh",
        up: {
          contentup: "上拉加载",
          contentrefresh: "正在加载...",
          contentnomore: '没有更多数据',
          callback: function () {
            getOrdersList();
          }
        }
      }
    });
    function getRTime(orderTime, order_no) {
      validCode = false;
      var t = setInterval(function () {
        orderTime--;
        var hh = parseInt(orderTime / 60 / 60 % 24);
        var mm = parseInt(orderTime / 60 % 60);
        var ss = parseInt(orderTime % 60);
        hh = hh < 10 ? ("0" + hh) : hh;   //时
        mm = mm < 10 ? ("0" + mm) : mm;   //分
        ss = ss < 10 ? ("0" + ss) : ss;   //秒

        $('#' + order_no).html('剩余' + hh + '时' + mm + '分' + ss + '秒')
        if (orderTime < 0) {
          clearInterval(t);
          window.history.go(0)

        }
      }, 1000);

    }

//									setTimeout(getRTime,1000)
    var getOrdersListTime = function (order_no) {
      $.ajax({
        url: __BATE__ + 'api/orderTime',
        data: {
          order_no: order_no
        },
        type: 'post',
        success: function (json) {
          $('#' + order_no).val(json.ret_msg),
            getRTime($('#' + order_no).val(), order_no)
        }
      })
    }


    var getOrdersList = function () {
      var flag = false;
      $.ajax({
        url: __BATE__ + 'api/customerOrderList',
        data: {
          type: type,
          page: page,
          token: tokenVal
        },
        type: 'post',
        success: function (json) {
          if (page <= json.pageAll) {
            if (page == 1) {
              $("#love-content").html();
            }
            ++page;

            var htmls = '';
            for (var i = 0; i < json.data.length; i++) {
              htmls += '<li class="mui-table-view-cell mui-media" data-id="' + json.data[i].order_no + '">' +
                '<p class=""><font>订单号：' + json.data[i].order_no + '</font>';

              var state = json.data[i].order_state;
              switch (state) {
                case 0:
                  htmls += '<font class="fr order-wait">待支付</font></p>';
                  break;
                case 1:
                  htmls += '<font class="fr order-wait">已支付</font></p>';
                  break;
                case 2:
                  htmls += '<font class="fr order-wait">已关闭</font></p>';
                  break;
                case 3:
                  htmls += '<font class="fr order-wait">退款处理中</font></p>';
                  break;
                case 4:
                  htmls += '<font class="fr order-wait">已退款</font></p>';
                  break;
                case 5:
                  htmls += '<font class="fr order-close">已发货</font></p>';
                  break;
                case 6:
                  htmls += '<font class="fr order-ok">已完成</font></p>';
                  break;
              }
              var hh = '';
              for (var k = 0; k < json.data[i].goods.length; k++) {
                hh += '<a class="go-order"  data-order="' + json.data[i].order_no + '">' +
                  '<img class="mui-media-object mui-pull-left" src="' + json.data[i].goods[k].productImage + '">' +
                  '<div class="mui-media-body">' + json.data[i].goods[k].goods_name + '</div>' +
                  '<div class="price-position"><span class="money">￥' + json.data[i].goods[k].price + '</span><span class="num">x' + json.data[i].goods[k].qty + '</span></div>' +
                  '</a>';
              }
              htmls = htmls + hh + '<div class="ordertt">合计：<span>￥' + json.data[i].order_amount + '</span>(含运费<span> ￥' + json.data[i].postage + '</span>)</div>';
              if (json.data[i].order_state == 0) {
                getOrdersListTime(json.data[i].order_no)
                htmls += '<div class="ordertt"><div class="fl timesss" id="' + json.data[i].order_no + '"></div><div  class="fr"><a class="pay" data-order="' + json.data[i].order_no + '">去支付</a><a class="go-comment active" data-order="' + json.data[i].order_no + '">去评价</a></div></div><input type="hidden" id="' + json.data[i].order_no + '" />';
              } else if (json.data[i].order_state == 1) {
                htmls += '<div class="ordertt"><div  class="fr"><a class="del" data-order="' + json.data[i].order_no + '">删除订单</a><a class="go-tracking" tracking-name="' + json.data[i].tracking-name + '" tracking-no="' + json.data[i].tracking-no + '">查看物流</a>'+
                '<a class="go-comment" data-order="' + json.data[i].order_no + '">去评价</a></div></div>';
              } else {
                htmls += '<div class="ordertt"><div  class="fr"><a class="del" data-order="' + json.data[i].order_no + '">删除订单</a><a class="go-tracking" tracking-name="中通" tracking-no="534957573294">查看物流</a>'+
                '<a class="go-comment active" data-order="' + json.data[i].order_no + '">去评价</a></div></div>';
              }

              '</li>';

            }
            var oldHtml = $("#orderProductList").html();
            $("#orderProductList").html(oldHtml + htmls);

          } else {
            flag = true;
          }

          try {
            mui('#pullrefresh').pullRefresh().endPullupToRefresh(flag);
          } catch (e) {
          }
        },
        error: function () {
          serverErr();
        }
      });

    };
    getOrdersList();
    mui.ready(function () {

      /*删除商品*/
      mui('body').on('tap', '.del', function (e) {
        var id = $(this).attr('data-order');
        var asd = $(this).parents('.mui-media');
        e.detail.gesture.preventDefault(); //修复iOS 8.x平台存在的bug，使用plus.nativeUI.prompt会造成输入法闪一下又没了
        var btnArray = ['否', '是'];
        mui.confirm('确认要删除此订单吗？', '', btnArray, function (e) {
          if (e.index == 1) {
            $.ajax({
              url: __BATE__ + 'api/deleteOrder',
              data: {
                order_no: id
              },
              type: 'post',
              success: function (json) {
                if (json.ret_msg == '删除完成') {
                  asd.remove();
                } else {
                  stip(json.ret_msg)
                }
              },
              error: function () {
                serverErr();
              }
            });

          }
        })
      });

      $('body').on('tap', '.pay, .go-order', function () {
        openView('/user/order-info.html?id=' + $(this).attr('data-order'), 'order-info.html');
        return;
      });
      $('body').on('tap', '.go-comment', function () {
        openView('/user/comment-list.html?id=' + $(this).attr('data-order'), 'comment-list.html');
        return;
      });
      $('body').on('tap', '.go-tracking', function () {
        openView('https://m.kuaidi100.com/index_all.html?type=' + $(this).attr('tracking-name') + '&postid='+ $(this).attr('tracking-no') + '&callbackurl=http://192.168.1.133:9000/user/order-list.html', 'order-info.html');
        return;
      });

    });

  }(mui, document));
</script>
</body>

</html>
